package com.xy.novelmanagement.service.impl;

import com.github.pagehelper.PageHelper;
import com.xy.model.Comment;
import com.xy.model.PageList;
import com.xy.model.Readers;
import com.xy.novelmanagement.mapper.CommentManagementMapper;
import com.xy.novelmanagement.service.ICommentManagementService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class CommentManagementServiceImpl implements ICommentManagementService {
		@Autowired
		private CommentManagementMapper commentManagementMapper;

		@Override
		public PageList<Comment> getCommentList(Integer pageNum, Integer pageSize, String overTime) {
				pageNum = pageNum == null ? 1 : pageNum;
				pageSize = pageSize == null ? 50 : pageSize;
				overTime = overTime == null || overTime.equals("") ? "7" : overTime;
				PageHelper.startPage(pageNum, pageSize);
				List<Comment> list = commentManagementMapper.selectAllComments(overTime);
				return new PageList<>(list);
		}

		@Override
		@Transactional
		public Integer deleteComments(List<Comment> commentList) {
				Integer code = 1;
				try {
						for (Comment com : commentList) {
								Readers author = com.getCommentAuthor();
								if (author.getNickName() != null || !author.getNickName().equals("")) {
										log.info("CommentManagementServiceImpl deleteComments author = " + author);
										commentManagementMapper.deleteUserCommentsById(com.getId());
										commentManagementMapper.deleteById(com.getId());
										commentManagementMapper.deleteBookComment(com.getId());
								}
						}
				} catch (Exception e) {
						log.error("setAllAutoPlay 错误消息：{}", e.getMessage(), e);
						return 0;
				}
				return code;
		}
}
